package se2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

// Created on:	2-Jun-08
// Author:		Richard Shih

public class Message
{
	private int					id;
	private GregorianCalendar	date;
	private String				subject;
	private String				body;
	private boolean				viewed;
	private String				dateString;
	private int					numNewMessages;

	public Message()
	{
	}

	public Message(int id)
	{
		this.id = id;
		this.getMessageFromDB(this.id);
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public GregorianCalendar getDate()
	{
		return date;
	}

	public void setDate(GregorianCalendar date)
	{
		this.date = date;
	}

	public String getSubject()
	{
		return subject;
	}

	public void setSubject(String subject)
	{
		this.subject = subject;
	}

	public String getBody()
	{
		return body;
	}

	public void setBody(String body)
	{
		this.body = body;
	}

	public boolean isViewed()
	{
		return viewed;
	}

	public void setViewed(boolean viewed)
	{
		this.viewed = viewed;
	}
	
	public String getDateString() {
		return dateString;
	}

	public void setDateString(String dateString) {
		this.dateString = dateString;
	}

	public String getStatus()
	{
		if (this.viewed)
		{
			return "Read";
		}
		else
		{
			return "Unread";
		}
	}
	
	public int getNumNewMessages() {
		return numNewMessages;
	}

	public void setNumNewMessages(int numNewMessages) {
		this.numNewMessages = numNewMessages;
	}
	
	public void setMessageStatus(boolean read) {
		
		String s;
		
		if (read){
			this.viewed = true;
			s = "true";
		} 
		else
		{
			this.viewed = false;
			s = "false";
		}
		
		try
		{			
			Connection conn = getConnection();
			PreparedStatement ps;
			
			ps = conn.prepareStatement("UPDATE messages SET viewed = ? WHERE id = ?");
			ps.setString(1, s);
			ps.setInt(2, this.id);
			ps.executeUpdate();
			
			conn.close();
			ps.close();
		}
		catch(Exception e)
		{
			System.out.println("Message.java - Error updating balance");
			System.out.println(e);
		}
		
	}

	private Connection getConnection() throws ClassNotFoundException, SQLException
	{
		Class.forName("org.sqlite.JDBC");
		String home = System.getProperty("user.home");
		Connection conn = DriverManager.getConnection("jdbc:sqlite:" + home + "/db/VoIP");
		
		return conn;
	}

	public boolean newMail()
	{
		numNewMessages = 0;
		
		try
		{
			Connection conn = getConnection();
			PreparedStatement ps;
			
			ps = conn.prepareStatement("select * from messages where viewed = 'false'");
			
			// Load all the billing plans
			ResultSet rs = ps.executeQuery();
			while (rs.next())
			{
				numNewMessages++;
			}
			rs.close();
			ps.close();
			conn.close();
			
			if (numNewMessages == 0)
			{
				return false;
			} 
			else 
			{
				return true;
			}
		}
		catch (Exception e)
		{
			System.out.println("Query Error in BillingPlan.java - getAllBillingPlans()");
			return false;
		}
	}
	
	public List<Message> getAllMessages()
	{		
		try
		{
			Connection conn = getConnection();
			PreparedStatement ps;
			
			ps = conn.prepareStatement("select * from messages order by viewed");
			
			List<Message> messages = new ArrayList<Message>();
			
			// Load all the billing plans
			ResultSet rs = ps.executeQuery();
			while (rs.next())
			{
				Message m = new Message();
				m.setId(rs.getInt(1));
				m.setSubject(rs.getString(2));
				m.setBody(rs.getString(3));
				m.setDateString(rs.getString(4));
				m.setViewed(rs.getString(5).equalsIgnoreCase("true"));
				messages.add(m);
			}
			
			rs.close();
			ps.close();
			conn.close();
			
			return messages;
		}
		catch (Exception e)
		{
			System.out.println("Query Error in BillingPlan.java - getAllBillingPlans()");
			return null;
		}
	}
	
	public void getMessageFromDB(int id)
	{		
		try
		{
			Connection conn = getConnection();
			PreparedStatement ps;
			List<Message> messages = new ArrayList<Message>();
			
			ps = conn.prepareStatement("select * from messages where id = ? ");
			ps.setInt(1, this.id);
			ResultSet rs = ps.executeQuery();
			
			if (rs.next())
			{
				this.setId(rs.getInt(1));
				this.setSubject(rs.getString(2));
				this.setBody(rs.getString(3));
				this.setDateString(rs.getString(4));
				this.setViewed(rs.getString(5).equalsIgnoreCase("true"));
			}
			
			rs.close();
			ps.close();
			conn.close();
			
		}
		catch (Exception e)
		{
			System.out.println("Query Error in Message.java - getMessageFromDB()");
		}
	}
	
}
